package com.mytest.leetcode;

import com.alibaba.fastjson.JSON;
import jdk.nashorn.internal.parser.JSONParser;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by shixi  on 2019/6/24
 */
@Slf4j
public class Solution206 {

    public static void main(String[] args) {
        Solution206 solution = new Solution206();

        ListNode listNode = new ListNode(1);
        ListNode listNode2 = new ListNode(2);
        ListNode listNode3 = new ListNode(3);
        ListNode listNode4 = new ListNode(4);
        ListNode listNode5 = new ListNode(5);

        listNode.next = listNode2;
        listNode2.next = listNode3;
        listNode3.next = listNode4;
        listNode4.next = listNode5;

        ListNode reverse = solution.reverseList(listNode);
        log.info("reverse:{}", JSON.toJSON(reverse).toString());

    }

//    public ListNode reverseList(ListNode head) {
//        ListNode result = new ListNode(0);
//        this.reverse(head,result);
//        return result.next;
//    }

    public ListNode reverse(ListNode head, ListNode listNode) {
        if(head.next != null){
            reverse(head.next, listNode);
        }
        log.info("val {}", head.val);
        ListNode node = new ListNode(head.val);
        listNode.next = node;
        return listNode;
    }

    public ListNode reverseList(ListNode head) {
        if (head == null || head.next == null) {
            return head;
        }
        ListNode newHead = reverseList(head.next);
        head.next.next = head;
        head.next = null;
        return newHead;
    }









    public ListNode reverse(ListNode head) {

        if (head.next == null) {
            return head;
        }
        ListNode next = head.next;
        reverse(next.next);
        head = next.next;
        head.next = null;

        return head;
    }
}